package com.xx.pmis.web.actions;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.xx.pmis.model.Social;
import com.xx.pmis.model.SocialBudget;
import com.xx.pmis.model.util.SocialMonth;
import com.xx.pmis.services.SocialBudgetService;
import com.xx.pmis.services.SocialService;
import com.xx.pmis.web.forms.SocialForm;
import com.xx.pmis.web.util.AccountUtil;
import com.xx.pmis.web.util.CurrentDate;
import com.xx.pmis.web.util.SocialUtil;

/**
 * @class 社交Action类 <br>
 * 
 * @editor 石星星 @Date 2011-9-21 下午03:17:42 <br>
 * @version jdk_1.5.0
 */
public class SocialAction extends PMISBaseAction {
	private static final Log log = LogFactory.getLog(SocialAction.class);

	SocialBudgetService socialBudgetService = getPMISServices().getSocialBudgetService();
	SocialService socialService = getPMISServices().getSocialService();

	AccountUtil accountUtil = new AccountUtil();
	SocialUtil socialUtil = new SocialUtil();

	// 社交
	public ActionForward listSocial(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<SocialMonth> list = socialUtil.getSocialOfTwelveMonths();
		request.setAttribute("socialList", list);
		request.setAttribute("position", "物资管理/社交");
		return mapping.findForward("listSocial");
	}

	// 每月社交支出明细
	public ActionForward listSocialOfMonth(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		request.setAttribute("position", "社交/月支出明细");
		return mapping.findForward("listSocialOfMonth");
	}

	// 新增
	public ActionForward addSocial(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		request.setAttribute("socialDate", CurrentDate.currentDate());
		request.setAttribute("position", "社交/新增");
		return mapping.findForward("addSocial");
	}

	// 新增，处理方法
	public ActionForward addSocial_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		SocialForm f = (SocialForm) form;
		String socialName = f.getSocialName(); // 社交名称
		String socialType = f.getSocialType(); // 请客0，送礼 1
		String socialDate = f.getSocialDate(); // 消费日期，结算时作为流水账的记账日期
		String socialExpense = f.getSocialExpense(); // 费用
		String socialDesc = f.getSocialDesc(); // 描述

		Social social = new Social();
		social.setSocialName(socialName);
		social.setSocialType(socialType);
		social.setSocialDate(socialDate);
		social.setSocialExpense(socialUtil.if1ZeroTo2Zero(socialExpense));
		social.setSocialDesc(socialDesc);
		social.setSocialPay("1");

		try {
			socialService.saveOrUpdateSocial(social);
		} catch (Exception e) {
			String errorPage = "【物资管理/社交/新增】保存【社交】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		listSocialOfMonthByPage(mapping, form, request, response);
		return mapping.findForward("listSocialOfMonthByPage");
	}

	// 编辑
	public ActionForward editSocial(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String socialID = request.getParameter("socialID");

		Social social = new Social();
		try {
			social = socialService.getSocialBySocialID(socialID);
		} catch (Exception e) {
			String errorPage = "【物资管理/社交/编辑】根据【社交ID}查询该【社交】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		request.setAttribute("social", social);
		request.setAttribute("position", "社交/编辑");
		return mapping.findForward("editSocial");
	}

	// 编辑，处理方法
	public ActionForward editSocial_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		SocialForm f = (SocialForm) form;
		String socialID = f.getSocialID(); // 社交名称
		String socialName = f.getSocialName(); // 社交名称
		String socialType = f.getSocialType(); // 请客0，送礼 1
		String socialDate = f.getSocialDate(); // 消费日期，结算时作为流水账的记账日期
		String socialExpense = f.getSocialExpense(); // 费用
		String socialDesc = f.getSocialDesc(); // 描述

		Social social = null;
		try {
			social = socialService.getSocialBySocialID(socialID);
		} catch (Exception e) {
			String errorPage = "【物资管理/社交/编辑】根据【社交ID}查询该【社交】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		social.setSocialName(socialName);
		social.setSocialType(socialType);
		social.setSocialDate(socialDate);
		social.setSocialExpense(socialUtil.if1ZeroTo2Zero(socialExpense));
		social.setSocialDesc(socialDesc);

		try {
			socialService.saveOrUpdateSocial(social);
		} catch (Exception e) {
			String errorPage = "【物资管理/社交/编辑】保存【社交】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		listSocialOfMonthByPage(mapping, form, request, response);
		return mapping.findForward("listSocialOfMonthByPage");
	}

	// 删除
	public ActionForward delSocial(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		Social social = null;
		SocialForm f = (SocialForm) form;
		String[] ids = f.getIds();
		for (int i = 0; i < ids.length; i++) {
			try {
				social = socialService.getSocialBySocialID(ids[i]);
			} catch (Exception e) {
				String errorPage = "【物资管理/社交/删除】根据【社交ID}查询该【社交】异常！" + e.toString();
				log.info(errorPage);
				request.setAttribute("errorPage", errorPage);
				request.setAttribute("position", "出错了");
				return mapping.findForward("errorPage");
			}
			try {
				socialService.deleteSocial(social);
			} catch (Exception e) {
				String errorPage = "【物资管理/社交/删除】保存【社交】异常！" + e.toString();
				log.info(errorPage);
				request.setAttribute("errorPage", errorPage);
				request.setAttribute("position", "出错了");
				return mapping.findForward("errorPage");
			}
		}

		listSocialOfMonthByPage(mapping, form, request, response);
		return mapping.findForward("listSocialOfMonthByPage");
	}

	// 分页显示
	public ActionForward listSocialOfMonthByPage(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String socialDate = request.getParameter("socialDate");
		String month = socialDate.substring(5, 7);
		String year = socialDate.substring(0, 4);

		int PAGESIZE = 10;// 每页记录数
		int totalPage = 1;// 总页数

		// 查询每月总社交记录数
		int count = 0;
		try {
			count = socialService.getCountOfSocialOfMonth(year, month);
		} catch (Exception e) {
			String errorPage = "【物资管理/社交】查询【社交】数目异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("count", count);
		if (count % PAGESIZE == 0) {
			totalPage = count / PAGESIZE;
		} else {
			totalPage = count / PAGESIZE + 1;
		}
		// 2012-08-28 修复如果数据量为0，总页数为0的情况
		if (totalPage == 0) {
			totalPage = 1;
		}
		request.setAttribute("totalPage", totalPage);
		// 声明要翻的页
		String pageNum = "1";
		try {
			String thisPageNum = request.getParameter("pageNum");
			// 如果传了页码参数
			if (thisPageNum != null && thisPageNum.length() != 0) {
				pageNum = thisPageNum;
			}
		} catch (RuntimeException e) {
			log.info("" + e.toString());
		}
		log.info("page:" + pageNum);
		request.setAttribute("pageNum", pageNum);
		List<Social> all = null;
		try {
			all = socialService.getAllSocialOfMonthByPage(Integer.parseInt(pageNum), PAGESIZE, year, month);
		} catch (Exception e) {
			String errorPage = "【物资管理/社交】查询【社交】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("socialList", all);
		request.setAttribute("PAGESIZE", PAGESIZE);
		// 如果要翻的这页是第一页
		if ("1".equals(pageNum)) {
			// 设置noUp为yes(界面上用此控制'首页'与'上一页'按钮失效)
			request.setAttribute("noUp", "yes");
		}

		// 如果要翻的这页是最后页
		if (totalPage == Integer.parseInt(pageNum)) {
			// 设置noDown为yes(界面上用此控制'下一页'与'尾页'按钮失效)
			request.setAttribute("noDown", "yes");
		}

		request.setAttribute("socialDate", socialDate);
		request.setAttribute("action", "social.do");
		request.setAttribute("position", "社交/" + month.replace("0", "") + "月社交支出明细");
		return mapping.findForward("listSocialOfMonthByPage");
	}

	// 编辑，社交预算
	public ActionForward editSocialBudget(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String socialDate = request.getParameter("socialDate");
		SocialBudget socialBudget = null;
		try {
			socialBudget = socialBudgetService.getSocialBudgetByBudgetDate(socialDate);
		} catch (Exception e) {
			String errorPage = "【物资管理/社交】根据【日期/YYYY-MM】查询该月的【社交预算】异常！" + e.toString();
			log.info(errorPage);
		}
		if (null == socialBudget) {
			socialBudget = new SocialBudget();
			request.setAttribute("budgetExpense", "0.00");
			request.setAttribute("budgetDesc", "");
		} else {
			request.setAttribute("budgetExpense", socialBudget.getBudgetExpense());
			request.setAttribute("budgetDesc", socialBudget.getBudgetDesc());
		}
		request.setAttribute("socialDate", socialDate);
		request.setAttribute("position", "编辑/预算");
		return mapping.findForward("editSocialBudget");
	}

	// 编辑，社交预算，处理方法
	public ActionForward editSocialBudget_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		SocialForm f = (SocialForm) form;
		String budgetExpense = f.getBudgetExpense();
		String budgetDesc = f.getBudgetDesc();
		String budgetDate = f.getBudgetDate();

		SocialBudget socialBudget = new SocialBudget();
		try {
			socialBudget = socialBudgetService.getSocialBudgetByBudgetDate(budgetDate);
		} catch (Exception e) {
			String errorPage = "【物资管理/社交】根据【日期/YYYY-MM】查询该月的【社交预算】异常！" + e.toString();
			log.info(errorPage);
		}
		if (null == socialBudget) {
			socialBudget = new SocialBudget();
		}
		socialBudget.setBudgetDate(budgetDate);
		socialBudget.setBudgetExpense(socialUtil.if1ZeroTo2Zero(budgetExpense));
		socialBudget.setBudgetDesc(budgetDesc);
		socialBudget.setBudgetState("0");

		try {
			socialBudgetService.saveOrUpdateSocialBudget(socialBudget);
		} catch (Exception e) {
			String errorPage = "【物资管理/社交】保存【社交预算】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		listSocial(mapping, form, request, response);
		return mapping.findForward("listSocial");
	}

	// 结算
	public ActionForward paySocial(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String socialID = request.getParameter("socialID");
		String pageNum = request.getParameter("pageNum");
		Social social = null;
		try {
			social = socialService.getSocialBySocialID(socialID);
		} catch (Exception e) {
			String errorPage = "【物资管理/社交/结算】根据【社交ID】查询该【社交】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		// 生成记账凭证
		accountUtil.payExpenseToAccount(social.getSocialDate(), "0030016", "1", social.getSocialExpense(), "", social
				.getSocialName());
		// 更新结算状态，结算完成为"0"
		social.setSocialPay("0");
		try {
			socialService.saveOrUpdateSocial(social);
		} catch (Exception e) {
			String errorPage = "【物资管理/社交/结算】保存或更新【社交】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		String socialDate = social.getSocialDate().substring(0, 7);

		String month = socialDate.substring(5, 7);
		String year = socialDate.substring(0, 4);

		int PAGESIZE = 10;// 每页记录数
		int totalPage = 1;// 总页数

		// 查询每月总社交记录数
		int count = 0;
		try {
			count = socialService.getCountOfSocialOfMonth(year, month);
		} catch (Exception e) {
			String errorPage = "【物资管理/社交】查询【社交】数目异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("count", count);
		if (count % PAGESIZE == 0) {
			totalPage = count / PAGESIZE;
		} else {
			totalPage = count / PAGESIZE + 1;
		}
		// 2012-08-28 修复如果数据量为0，总页数为0的情况
		if (totalPage == 0) {
			totalPage = 1;
		}
		request.setAttribute("totalPage", totalPage);
		// 声明要翻的页
		// String pageNum = "1";
		try {
			String thisPageNum = request.getParameter("pageNum");
			// 如果传了页码参数
			if (thisPageNum != null && thisPageNum.length() != 0) {
				pageNum = thisPageNum;
			}
		} catch (RuntimeException e) {
			log.info("" + e.toString());
		}
		log.info("page:" + pageNum);
		request.setAttribute("pageNum", pageNum);
		List<Social> all = null;
		try {
			all = socialService.getAllSocialOfMonthByPage(Integer.parseInt(pageNum), PAGESIZE, year, month);
		} catch (Exception e) {
			String errorPage = "【物资管理/社交】查询【社交】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("socialList", all);
		request.setAttribute("PAGESIZE", PAGESIZE);
		// 如果要翻的这页是第一页
		if ("1".equals(pageNum)) {
			// 设置noUp为yes(界面上用此控制'首页'与'上一页'按钮失效)
			request.setAttribute("noUp", "yes");
		}

		// 如果要翻的这页是最后页
		if (totalPage == Integer.parseInt(pageNum)) {
			// 设置noDown为yes(界面上用此控制'下一页'与'尾页'按钮失效)
			request.setAttribute("noDown", "yes");
		}

		request.setAttribute("socialDate", socialDate);
		request.setAttribute("action", "social.do");
		request.setAttribute("position", "社交/" + month.replace("0", "") + "月社交支出明细");
		return mapping.findForward("listSocialOfMonthByPage");
	}

}